<!--
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-03-03 11:30:14
 * @Description  : 设置用户系统角色面板
-->

<template>
  <!-- <n-card
    :size="'small'"
    style="width: 540px"
    role="dialog"
    title="设置用户角色"
    :bordered="false"
    aria-modal="true"
  >
    <template #header-extra>
      <SvgIcon
        icon="chacha"
        class="cursor-pointer"
        @click="handleClose"
      ></SvgIcon>
    </template>
    <n-divider class="!my-2" /> -->
  <n-form
    ref="systemRoleFormRef"
    :label-width="isSmall ? 100 : 120"
    label-placement="left"
    :label-align="isSmall ? 'left' : 'right'"
    :model="user"
    size="small"
    require-mark-placement="right"
    :rules="formRule"
  >
    <n-form-item label="已选用户：" path="userIds">
      <UserSelect
        :root-dep-id="props.rootDepId"
        :has-not-dep-user="false"
        :default-user-ids="user.userIds"
        @change-value="handleUserIdsChange"
        class="!w-[300px]"
      ></UserSelect>
    </n-form-item>

    <n-form-item label="角色权限：" path="roleId">
      <RoleSelect
        :default-role-id="user.roleId"
        :multiple="false"
        @change-value="handleRoleIdChange"
      ></RoleSelect>
    </n-form-item>

    <n-form-item label=" " v-permission="[PERMISSIONS.USER_EDIT]">
      <n-button type="primary" class="grent-btn w-[120px]" @click="handleSave">
        保存
      </n-button>
    </n-form-item>
  </n-form>
  <!-- </n-card> -->
</template>

<script setup lang="ts">
import type { FormInst } from 'naive-ui'
import type { SetUserRoleReq } from '@/types/user'
import { userMenuShow } from '@/hooks/useMenuShow'
import { PERMISSIONS } from '@/constants/permission'
import type { IndexObj } from '@/types/commom'

const emits = defineEmits<{
  (e: 'visibleChange', value: boolean): void
  (e: 'setUserRole', data: SetUserRoleReq): void
}>()

const props = defineProps<{
  checkUserIds: string[]
  rootDepId?: string
}>()

const handleClose = () => {
  emits('visibleChange', false)
}

const { isSmall } = userMenuShow()

const user = reactive<IndexObj>({
  roleId: '',
  userIds: []
})

const initUser = () => {
  user.userIds = props.checkUserIds
  user.roleId = '3'
}

onMounted(() => {
  initUser()
})

const handleRoleIdChange = (value: string | string[]) => {
  user.roleId = value as string
}

const handleUserIdsChange = (value: string[] | string) => {
  // console.log(value)
  user.userIds = value as string[]
  // .map(
  //   (item) => item.split(COMMON_SEPARATOR)[1]
  // )
}

const formRule = {
  roleId: [
    {
      required: true,
      message: '请选择用户角色',
      trigger: ['blur', 'change']
    }
  ]
  // userIds: [
  //   {
  //     required: true,
  //     message: '请选择用户',
  //     trigger: ['blur', 'change']
  //   }
  // ]
}

const systemRoleFormRef = ref<FormInst | null>(null)

const handleSave = () => {
  systemRoleFormRef.value?.validate((error: any) => {
    if (error) {
      return
    }

    emits('setUserRole', {
      roleId: user.roleId,
      userIds: user.userIds
    })
  })
}
</script>

<style scoped lang="scss"></style>
